package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import com.ruoyi.system.controller.standard.SSEController;
import com.ruoyi.system.domain.dto.StartSpiderDto;
import com.ruoyi.system.domain.standard_relevant.StandardExcel;
import com.ruoyi.system.mapper.standard.StandardExcelMapper;
import com.ruoyi.system.util.SpiderResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.StandardSpiderMapper;
import com.ruoyi.system.domain.StandardSpider;
import com.ruoyi.system.service.IStandardSpiderService;

/**
 * 爬虫管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-21
 */
@Service
public class StandardSpiderServiceImpl implements IStandardSpiderService
{
    @Autowired
    private StandardSpiderMapper standardSpiderMapper;

    @Autowired
    private StandardExcelMapper standardExcelMapper;

    @Autowired
    private SSEController sseController;
    /**
     * 查询爬虫管理
     *
     * @param id 爬虫管理主键
     * @return 爬虫管理
     */
    @Override
    public StandardSpider selectStandardSpiderById(Long id)
    {
        return standardSpiderMapper.selectStandardSpiderById(id);
    }

    /**
     * 查询爬虫管理列表
     *
     * @param standardSpider 爬虫管理
     * @return 爬虫管理
     */
    @Override
    public List<StandardSpider> selectStandardSpiderList(StandardSpider standardSpider)
    {
        return standardSpiderMapper.selectStandardSpiderList(standardSpider);
    }

    /**
     * 新增爬虫管理
     *
     * @param standardSpider 爬虫管理
     * @return 结果
     */
    @Override
    public int insertStandardSpider(StandardSpider standardSpider)
    {
        return standardSpiderMapper.insertStandardSpider(standardSpider);
    }

    /**
     * 修改爬虫管理
     *
     * @param standardSpider 爬虫管理
     * @return 结果
     */
    @Override
    public int updateStandardSpider(StandardSpider standardSpider)
    {
        return standardSpiderMapper.updateStandardSpider(standardSpider);
    }

    /**
     * 批量删除爬虫管理
     *
     * @param ids 需要删除的爬虫管理主键
     * @return 结果
     */
    @Override
    public int deleteStandardSpiderByIds(Long[] ids)
    {
        return standardSpiderMapper.deleteStandardSpiderByIds(ids);
    }

    /**
     * 删除爬虫管理信息
     *
     * @param id 爬虫管理主键
     * @return 结果
     */
    @Override
    public int deleteStandardSpiderById(Long id)
    {
        return standardSpiderMapper.deleteStandardSpiderById(id);
    }
    @Override
    @Async
    public CompletableFuture<Void> calSpider(StartSpiderDto startSpiderDto)  {

        return CompletableFuture.runAsync(() -> {
            try {
                //把startSpiderDto中的standardCodes['xxx']和params['A11',...]传给tools工具调用pythonApi接口，返回StandardSpider对象集合[{standardCode:xxx,A11:xx,...},standardCode:xxx,A11:xx,...}]
                SpiderResponseUtil spiderResponseUtil = new SpiderResponseUtil();
                List<Map<String, Object>> calAns = spiderResponseUtil.sendSpiderRequest(startSpiderDto);
                if(calAns.size()==0){
                    sseController.sendMessage("爬虫失败");
                }
//            System.out.println("calAns:"+calAns);
                List<StandardSpider> resList = spiderResponseUtil.convertToStandardSpiderList(calAns);

//            System.out.println("resList:"+resList);
                //遍历resList,若standardCode不存在，则插入数据库,若存在，则更新数据库
                if(resList.size()!=0) {
                    for (StandardSpider standardSpider : resList) {
                        //初始设置为未审核状态
                        standardSpider.setStatus(0L);
                        if (standardSpiderMapper.selectByStandardCode(standardSpider.getStandardCode()) == null) {
                            standardSpiderMapper.insertStandardSpider(standardSpider);
                            sseController.sendMessage("采集标准"+standardSpider.getStandardCode()+"部分指标成功");
                        } else {
                            Long id = standardSpiderMapper.selectByStandardCode(standardSpider.getStandardCode()).getId();
                            standardSpider.setId(id);
                            standardSpiderMapper.updateStandardSpider(standardSpider);
                            sseController.sendMessage("采集更新标准"+standardSpider.getStandardCode()+"部分指标成功");
                        }
                    }
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
    }

    @Override
    public int auditStandardSpider(StandardSpider standardSpider) {

        int msg;
        //if(standardSpider.getStatus()==1)
        //审核成功，则更新数据库
        if(standardSpider.getStatus()==1){
            msg=standardSpiderMapper.updateStandardSpider(standardSpider);
            //根据StandardSpider构建StandardExcel对象，并插入数据库
            StandardExcel standardExcel=new StandardExcel(standardSpider);
            Long id;
            //如果StandardExcel不存在，则插入数据库，否则更新数据库
            if(standardExcelMapper.selectStandardExcelByStandardCode(standardExcel.getStandardCode())==null){
                standardExcelMapper.insertStandardExcel(standardExcel);
            }
            else {
                id=standardExcelMapper.selectStandardExcelByStandardCode(standardExcel.getStandardCode()).getId();
                standardExcel.setId(id);
                standardExcelMapper.updateStandardExcel(standardExcel);
            }
        }
            //standardExcelMapper.insertStandardExcel(standardExcel);
        else{
            msg=standardSpiderMapper.updateStandardSpider(standardSpider);
        }
        return msg;
    }
}
